/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.smg.community.service;

import java.util.List;

import javacommon.base.BaseManager;
import javacommon.base.EntityDao;

import org.springframework.stereotype.Service;

import com.smg.community.dao.CrmRightDao;
import com.smg.community.model.CrmRight;
import com.smg.community.util.StringUtil;
import com.smg.community.vo.query.CrmRightQuery;

import cn.org.rapid_framework.page.Page;


/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
public class CrmRightManager extends BaseManager<CrmRight,java.lang.Integer>{

	private CrmRightDao crmRightDao;
	private static final String KEY = "E";
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCrmRightDao(CrmRightDao dao) {
		this.crmRightDao = dao;
	}
	public EntityDao<CrmRight, Integer> getEntityDao() {
		return this.crmRightDao;
	}
	
	/**
	 * 根据角色id获取对应的权限
	 * */
	public List<CrmRight> getAllRightByRoleId(String roleIds,String parentCode){
		return crmRightDao.getAllRightByRoleId(roleIds,parentCode);
	}
	/**
	 * 根据条件获取对应权限
	 * */
	public CrmRight getBy(CrmRightQuery query){
		return crmRightDao.getBy(query);
	}
	/**
	 * 获取所有权限
	 * */
	public List<CrmRight> getAll(){
		return crmRightDao.getAll();
	}
	
	/**
	 * 分页查询权限列表
	 * @param query 封装参数
	 * @return
	 */
	public Page<CrmRight> findPage(CrmRightQuery query) {
		// 去空格
		if (!StringUtil.isEmpty(query.getCode())) {
			query.setCode(query.getCode().trim());
		}
		if (!StringUtil.isEmpty(query.getText())) {
			query.setText(query.getText().trim());
		}
		if (!StringUtil.isEmpty(query.getRightDesc())) {
			query.setRightDesc(query.getRightDesc().trim());
		}
		// 只查询父级权限
		query.setParentCode("0");
		return crmRightDao.findPage(query);
	}
	
	/**
	 * 移动排序大小
	 * @param id	主键
	 * @param sortCount 移动位数
	 * @return
	 */
	public String updateSort(String id, String sortCount) {
		
		// 验证权限是否存在
		CrmRight crmRight = checkRightPer(Integer.parseInt(id.trim()));
		// 获取父级权限
		String parentCode = crmRight.getParentCode();
		// 获取当前位置
		int corruntPlace = crmRight.getRightSort();
		// 获取新位置
		int newPlace = corruntPlace + Integer.parseInt(sortCount);
		String retMessage = "";
		if (Integer.parseInt(sortCount) == 0 ) {
		} else if (Integer.parseInt(sortCount) > 0) { // -->靠近最大排序移动-向下移动
			// 获取最大的权限排序序号
			int maxPlace = crmRightDao.getMaxIndex(parentCode.trim());
			if (newPlace > maxPlace) {
				newPlace = maxPlace;
			}
			// 向下移动位置
			int count = crmRightDao.move(corruntPlace, newPlace, -1, parentCode.trim());
			retMessage = "您已向后移了" + (count - 1) +"个位置";
		} else { // --->靠近1移动--向上移动
			if (newPlace <= 0) {
				newPlace = 1;
			}
			// 向上移动位置
			int count = crmRightDao.move(newPlace, corruntPlace, 1, parentCode.trim());
			retMessage = "您已向前移了" + (count - 1) +"个位置";
		}
		
		// 更新当前位置
		crmRight.setRightSort(newPlace);
		crmRightDao.update(crmRight);
		return retMessage;
	}
	
	/**
	 * 新增权限
	 * @param text	权限名称
	 * @param rightDesc 权限描述
	 * @param parentId 父级权限主键
	 * @param url 路径
	 * @return
	 */
	public String addRightPer(String text, String rightDesc, String parentId, String url)
	 {
		// 基本参数判断
		checkParam(text, rightDesc);
		// 验证权限名是否存在
		checkText(text.trim());
		// 获取父级权限主键
		int parentRId = 0;
		if (!StringUtil.isEmpty(parentId)) {
			parentRId = Integer.parseInt(parentId);
		}
		String parentCode = null;
		if (parentRId > 0) { // 添加子权限
			// 验证父级权限是否存在
			CrmRight parentCrmRight = checkRightPer(parentRId);
			// 获取父级权限code
			parentCode = parentCrmRight.getCode();
		} else {
			parentCode = "0";
		}
		
		// 生成code 和顺序
		Object[] retMsg = generateCode(parentCode);
		String code = (String) retMsg[0];
		int rightSort = (Integer) retMsg[1];
		// 保存权限
		save(code, parentCode, url, rightDesc, rightSort, text);
		return "新增权限：“" + text + "”成功！";
	}
	
	/**
	 * 获取子级菜单权限
	 * @param id 主键
	 * @return
	 */
	public List<CrmRight> selectChidrenPers(String id)  {
		// 参数判断
		if (StringUtil.isEmpty(id)) {
		}
		// 获取父级对象
		CrmRight crmRight = checkRightPer(Integer.parseInt(id));
		return crmRightDao.findChidrenPers(crmRight.getCode());
	}
	
	// 验证权限是否存在
	private CrmRight checkRightPer (int id)  {
		if (id < 1) {
		}
		// 获取记录
		CrmRight crmRight = (CrmRight) crmRightDao.getById(id);
		if (crmRight == null) {
		}
		return crmRight;
	}
	
	/**
	 * 基本参数校验
	 * @param text 名称
	 * @param rightDesc 描述
	 * @throws ParamException
	 */
	private void checkParam (String text, String rightDesc)  {
		if (StringUtil.isEmpty(text)) {
		}
		if (text.trim().length() > 50) {
		}
		if (!StringUtil.isEmpty(rightDesc) && rightDesc.trim().length() > 50) {
		}
	}
	
	/**
	 * 生成自身code
	 * @param parentCode 父级code
	 */
	private Object[] generateCode(String parentCode) 
	 {
		
		StringBuffer codeSb = new StringBuffer(); 
		int maxRightSort = 0;
		// 获取最大的code和最大的rightSort
		CrmRight crmRight = crmRightDao.getMaxCodeAndSort(parentCode);
		if (crmRight == null) { 
			if (!"0".equals(parentCode)) { // 说明此二级菜单下无子菜单
				codeSb.append(parentCode).append("01");
			} else {// 说明此此菜单是第一个一级菜单
				codeSb.append("L01");
			}
		}  else {
			String maxCode = crmRight.getCode();
			maxRightSort = crmRight.getRightSort();
			// 获取code最后的两位比如L01则获取01
			String lastTwo = maxCode.substring(maxCode.length()-2 , maxCode.length());
			int addCount = Integer.parseInt(lastTwo) + 1;
			if (addCount < 10) {
				codeSb.append(maxCode.subSequence(0, maxCode.length() - 2)).append("0").append(addCount);
			} else {
				codeSb.append(maxCode.subSequence(0, maxCode.length() - 2)).append(addCount);
			}
			
		}
		return new Object[]{codeSb.toString(), maxRightSort + 1};
	}
	
	/**
	 * 保存权限
	 * @param code 
	 * @param parentCode
	 * @param rightDesc
	 * @param rightSort
	 * @param text
	 */
	private void save(String code, String parentCode, String url,
			String rightDesc, int rightSort, String text) {
		CrmRight crmRight = new CrmRight();
		crmRight.setCode(code.trim());
		crmRight.setParentCode(parentCode.trim());
		crmRight.setRightDesc(rightDesc.trim());
		crmRight.setRightSort(rightSort);
		crmRight.setText(text.trim());
		crmRight.setType("menu");
		crmRight.setUrl(url.trim());
		crmRightDao.save(crmRight);
	}
	
	/**
	 * 验证权限名称是否存在
	 * @return 
	 */
	private void checkText (String text)  {
		CrmRight crmRight = crmRightDao.getByText(text);
		if (crmRight != null) {
		}
	}
}
